package org.nightrunner.d3.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.GuideInfo;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.service.IGuideInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Index Controller
 * 
 * @author NigthRunner
 * @date 2012-7-24
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/")
public class IndexController implements CRUDConstant {

	private static final Logger LOGGER = SysLog.CONTROLLER;

	public static final String RETURN_VIEW_PREFIX = "/";

	@Autowired
	private IGuideInfoService guideInfoService;

	public IGuideInfoService getGuideInfoService() {
		return guideInfoService;
	}

	public void setGuideInfoService(IGuideInfoService guideInfoService) {
		this.guideInfoService = guideInfoService;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletResponse response, ModelMap modelMap)
			throws Exception {
		return RETURN_VIEW_PREFIX + "index";
	}

	@RequestMapping(value = "guideInfo", method = RequestMethod.GET)
	public String guideInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {

		GuideInfo guideInfo = new GuideInfo();
		guideInfo.setIp(request.getRemoteAddr());
		guideInfo.setUserAgent(request.getHeader("User-Agent"));
		guideInfoService.addGuideInfo(guideInfo);

		if (LOGGER.isInfoEnabled()) {
			LOGGER.info(guideInfo);
		}
		return RETURN_VIEW_PREFIX + "guideInfo";
	}
}